#!/usr/bin/env python 2.7
# -*- coding: utf-8 -*-
import json
import re
import sys
import datetime

reload(sys)
sys.setdefaultencoding('utf-8')

'''eof
name:额度定价个人征信负债
code:pricing_amount_ICindex
tableName:
columnName:
groups:个人征信模块
dependencies:IC_DSJ
type:常用指标
datasourceType:在线指标
description:
eof'''


def formatData(table_Name):
    """
    获取数据
    :param table_Name:字典keys
    :return:[{}]
    """
    try:
        ic_data = IC_DSJ["data"].get(table_Name)
        return ic_data if isinstance(ic_data, list) and len(ic_data) > 0 else [{}]

    except:
        return [{}]


def ConvertDataType(dataValue, dataType):
    returnData = None
    if dataType == 'float':
        try:
            returnData = float(dataValue) #if float(dataValue) >= 0 else 0
        except:
            returnData = 0
    elif dataType == 'None':
        returnData = 0 if dataValue is None else dataValue
    return returnData

def isfloat(x):

    try:
        if isinstance(float(x), float) and float(x)>=0:
            if str(x) in ['inf', 'infinity', 'INF', 'INFINITY', 'True']:
                return False
            else:
                return True
        else:
            return False
    except:
        return False


def IC_balance():
    null_type_list = ['', None, 'null', 'Null', 'NULL', '/', ' ']
    try:
        # 贷款信息
        icrLoanInfo002Loan = formatData("ICRLoanInfo002*Loan")  # formatData 修改！！ 大小写问题！
        returnlist1 = []

        if icrLoanInfo002Loan == [{}] or icrLoanInfo002Loan in null_type_list:
            return 0
        else:
            for x in icrLoanInfo002Loan:
                #余额
                try:

                    Balance = ConvertDataType(x.get("balance"),'float')
                    # Balance 修改！！ 大小写问题！
                    LoanType = x.get('loanType')
                    if LoanType==u"个人经营性贷款":
                        returnlist1.append(Balance)
                    else:
                        returnlist1.append(0)
                except:
                    continue
        return sum(returnlist1)
    except:
        return 0

result = IC_balance()